#!/usr/bin/env node

/**
 * 检查tasks表数据的脚本
 * 使用方法: node scripts/check-tasks.js
 */

const { Pool } = require('pg')

// 数据库连接配置
const pool = new Pool({
  host: 'localhost',
  port: 5432,
  database: 'aide',
  user: 'postgres',
  password: 'unidt@123',
})

async function checkTasks() {
  const client = await pool.connect()
  
  try {
    console.log('🔍 检查 tasks 表的数据...')
    
    // 查看任务数量
    const countResult = await client.query('SELECT COUNT(*) FROM tasks')
    console.log(`📊 任务总数: ${countResult.rows[0].count} 条`)
    
    // 查看所有任务
    const tasksResult = await client.query(`
      SELECT id, title, priority, category, completed, created_at
      FROM tasks
      ORDER BY created_at DESC
    `)
    
    console.log('\n📋 任务列表:')
    if (tasksResult.rows.length === 0) {
      console.log('❌ 没有找到任何任务')
    } else {
      tasksResult.rows.forEach((task, index) => {
        const status = task.completed ? '✅ 已完成' : '⏳ 进行中'
        console.log(`${index + 1}. [${task.priority}] ${task.title} (${task.category}) - ${status}`)
      })
    }
    
    // 查看子任务
    console.log('\n🔍 子任务信息:')
    const subtasksResult = await client.query(`
      SELECT s.title, s.completed, t.title as task_title
      FROM subtasks s
      JOIN tasks t ON s.task_id = t.id
      ORDER BY t.title, s.title
    `)
    
    if (subtasksResult.rows.length === 0) {
      console.log('❌ 没有找到任何子任务')
    } else {
      subtasksResult.rows.forEach((subtask, index) => {
        const status = subtask.completed ? '✅' : '⏳'
        console.log(`${index + 1}. ${status} ${subtask.title} (属于: ${subtask.task_title})`)
      })
    }
    
  } catch (error) {
    console.error('❌ 检查任务失败:', error.message)
  } finally {
    client.release()
    await pool.end()
  }
}

// 运行脚本
checkTasks()
  .then(() => {
    console.log('\n🎉 任务检查完成')
    process.exit(0)
  })
  .catch((error) => {
    console.error('💥 任务检查失败:', error)
    process.exit(1)
  })
